<template>
  <div>
    <div class="crud-group">
      <el-input
        v-model="paramsForm.dbName"
        placeholder="库名"
      />
      <el-input
        v-model="paramsForm.tableName"
        placeholder="表名"
      />
      <el-select
        v-model="paramsForm.matchPattern"
        placeholder="匹配模式"
        clearable
      >
        <el-option
          label="模糊匹配"
          value="模糊匹配"
        />
        <el-option
          label="精准匹配"
          value="精准匹配"
        />
      </el-select>
      <el-select
        v-model="paramsForm.isDefaultRule"
        placeholder="是否所有库"
        clearable
      >
        <el-option
          label="是"
          value="是"
        />
        <el-option
          label="否"
          value="否"
        />
      </el-select>
      <el-select
        v-model="paramsForm.isAudit"
        placeholder="是否需要稽核"
        clearable
      >
        <el-option
          label="是"
          value="是"
        />
        <el-option
          label="否"
          value="否"
        />
      </el-select>
      <el-select
        v-model="paramsForm.isSerialAudit"
        placeholder="是否串行稽核"
        clearable
      >
        <el-option
          label="是"
          value="是"
        />
        <el-option
          label="否"
          value="否"
        />
      </el-select>

      <el-select
        v-model="paramsForm.isIgnore"
        placeholder="是否忽略异常"
        clearable
      >
        <el-option
          label="是"
          value="是"
        />
        <el-option
          label="否"
          value="否"
        />
      </el-select>

      <el-select
        v-model="paramsForm.isThreshold"
        placeholder="是否忽略阈值"
        clearable
      >
        <el-option
          label="是"
          value="是"
        />
        <el-option
          label="否"
          value="否"
        />
      </el-select>

      <el-button
        type="success"
        icon="el-icon-search"
        @click="getData"
      >搜索</el-button>
      <el-button
        type="warning"
        icon="el-icon-refresh-left"
        @click="reset"
      >重置</el-button>
      <el-button
        type="primary"
        icon="el-icon-plus"
        @click="()=>{
          dialogVisible = true
        }"
      >新增</el-button>
    </div>

    <el-table
      v-loading="isLoading"
      border
      :data="data"
    >
      <el-table-column
        label="库名"
        prop="dbName"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.dbName || '所有库' }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="表名"
        prop="tableName"
      />
      <el-table-column
        label="匹配模式"
        prop="matchPattern"
      />
      <el-table-column
        label="是否所有库"
        prop="isDefaultRule"
      />
      <el-table-column
        label="是否需要稽核"
        prop="isAudit"
      />
      <el-table-column
        label="是否串行稽核"
        prop="isSerialAudit"
      />
      <el-table-column
        label="是否忽略异常报错信息"
        prop="isIgnore"
      />
      <el-table-column
        label="是否忽略阈值"
        prop="isThreshold"
      />

      <el-table-column
        label="操作"
        width="100px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="()=>{
              selection = {...scope.row}
              dialogVisible = true
            }
            "
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            @click="()=>{
              deleteRow(scope.row.id)
            }"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        layout="prev, pager, next,sizes,total"
        :page-sizes="[5, 10, 20, 30, 40]"
        :current-page="paramsForm.current"
        :page-size="paramsForm.size"
        :total="total"
        @size-change="(val)=>{
          paramsForm.current = 1
          paramsForm.size = val
          getData()
        }"
        @current-change="(val)=>{
          paramsForm.current = val
          getData(false)
        }"
      />
    </div>

    <AddColdBLDialog
      :dialog-visible="dialogVisible"
      :edit-form="selection"
      @close="()=>{
        dialogVisible = false
        selection = null
        getData()
      }"
    />
  </div>
</template>

<script>
import cbApi from '@/api/coldStandby/index'
import AddColdBLDialog from './components/AddColdBLDialog.vue'

const form = {
  current: 1,
  size: 10,
  dbName: '',
  tableName: '',
  matchPattern: '',
  isDefaultRule: '',
  isIgnore: '',
  isAudit: '',
  isSerialAudit: '',
  isThreshold: ''
}
export default {
  components: {
    AddColdBLDialog
  },
  data() {
    return {
      data: [
        {
          tblDatabaseName: '1',
          tblName: '2',
          matchPattern: '3',
          isDefaultRule: '4',
          isIgnore: '5',
          isAudit: '6'
        }
      ],
      selection: null,
      dialogVisible: false,
      isLoading: false,
      total: 0,
      paramsForm: {...form}
    }
  },
  created() {
    this.getData()
  },
  methods: {
    getData(resetPage = true) {
      if (resetPage) {
        this.paramsForm.current = 1
      }
      this.isLoading = true
      cbApi.getAuditBlackList(this.paramsForm).then(res => {
        this.data = res.records
        this.total = res.total
      }).finally(() => {
        this.isLoading = false
      })
    },
    deleteRow(id) {
      this.$confirm('此操作将永久删除该条记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        cbApi.delAuditBlackList(id).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getData()
        })
      })
    },
    reset() {
      this.paramsForm = {...form}
      this.getData()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>

